"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
// 混入方法
function _mixin(target) {
    var mixinObj = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        mixinObj[_i - 1] = arguments[_i];
    }
    if (target == null) {
        console.warn("请检查混入的对象参数是否正确。");
        return false;
    }
    mixinObj.forEach(function (obj) {
        // 遍历混入对象的可枚举属性
        Object.keys(obj).forEach(function (key) {
            target[key] = obj[key];
        });
    });
    return target;
}
// 抓取DOM方法
function _getSingleDom(selector) {
    if (!selector) {
        console.warn("输入的选择器为空，请检查输入的选择器是否正确。");
        return;
    }
    if (typeof selector !== "string") {
        return selector;
    }
    var elem = document.querySelector(selector);
    if (elem) {
        return elem;
    }
    console.warn("没有获取到的元素，请检查输入的选择器是否正确。");
}
// 分页器类
var SplitPager = /** @class */ (function () {
    // 构造函数
    function SplitPager(options) {
        this.options = {};
        this.currentPage = this.options.currentPage || 1;
        this.pageScope = [];
        this.normalElem = [];
        this.htmlString = "";
        // 默认设置
        this.defaultOptions = {
            el: "",
            totalPage: 10,
            showPage: 5,
            currentPage: 1,
            pageQuery: "",
        };
        // 页面模板
        this.pageTemplate = {
            prevButton: '<button class = "pager-base pager-prev" p-mark = "prev">back</button>',
            nextButton: '<button class = "pager-base pager-next" p-mark = "next">next</button>',
            firstButton: '<button class = "pager-base pager-first" p-mark = "first">首页</button>',
            lastButton: '<button class = "pager-base pager-last" p-mark = "last">末页</button>',
            pageButton: '<button class = "pager-base pager-normal">?</button>',
        };
        this.rootPath = "";
        this.initOptions(options);
        this.initDom();
        this.initRootPath();
        this.spliceHtml();
        this.mountDom();
        this.setDomRef();
        this.calNumberScope();
        this.updateDomContent();
        this.setButtonAuthority();
        this.bindEvents();
    }
    // 初始化设置参数
    SplitPager.prototype.initOptions = function (options) {
        _mixin(this.options, this.defaultOptions, options);
    };
    // 初始化DOM元素
    SplitPager.prototype.initDom = function () {
        this.el = _getSingleDom(this.options.el);
    };
    // 初始化根路径
    SplitPager.prototype.initRootPath = function () {
        this.rootPath = location.pathname;
    };
    // 拼接html字符串
    SplitPager.prototype.spliceHtml = function () {
        var htmlContent = this.pageTemplate.firstButton + this.pageTemplate.prevButton; // 首页按钮+后退按钮
        for (var i = 0; i < this.options.showPage; i++) {
            // 拼接分页部分
            htmlContent = htmlContent + this.pageTemplate.pageButton;
        }
        htmlContent =
            htmlContent + this.pageTemplate.nextButton + this.pageTemplate.lastButton;
        this.htmlString = htmlContent;
    };
    // 挂载DOM元素
    SplitPager.prototype.mountDom = function () {
        if (typeof this.el === "undefined") {
            throw new Error("挂载DOM阶段失败，未找到真实存在的DOM元素。");
        }
        this.el.innerHTML = this.htmlString;
    };
    // 设置DOM引用
    SplitPager.prototype.setDomRef = function () {
        var _this_1 = this;
        var elemChildren = this.el.children;
        var hashMap = {
            prev: "prevElem",
            next: "nextElem",
            first: "firstElem",
            last: "lastElem",
        };
        Array.prototype.forEach.call(elemChildren, function (elem) {
            var markAttr = elem.getAttribute(SplitPager.mark);
            if (markAttr != null) {
                _this_1[hashMap[markAttr]] = elem;
            }
            else {
                _this_1.normalElem.push(elem);
            }
        });
    };
    // 计算数字范围
    SplitPager.prototype.calNumberScope = function () {
        if (this.options.showPage > this.options.totalPage) {
            throw new Error("输入的总页数小于展示页数，请检查输入的参数页数是否合理。");
        }
        this.pageScope = []; // 初始化页码范围
        var currentPage = this.currentPage;
        var _a = this.options, totalPage = _a.totalPage, showPage = _a.showPage; // 解构出总页数及展示按钮页数
        if (!totalPage || !showPage) {
            // 类型检查
            console.warn("请检查参数 showPage 或 totalPage 是否被正确输入。");
            return;
        }
        var start1 = Math.max(currentPage - Math.round(showPage / 2), 1);
        var end1 = Math.min(start1 + showPage - 1, totalPage);
        var end2 = Math.min(currentPage + Math.round(showPage / 2) - 1, totalPage);
        var start2 = Math.max(end2 - showPage + 1, 1);
        var start = Math.min(start1, start2);
        var end = Math.max(end1, end2);
        for (var i = start; i <= end; i++) {
            this.pageScope.push(i);
        }
        // console.log(this.pageScope, this.currentPage);
        // if (this.currentPage === 1) {
        //   // 当前为首页时
        //   let endPage: number = Math.min(
        //     this.options.showPage as number,
        //     this.options.totalPage as number
        //   );
        //   for (let i = 1; i <= endPage; i++) {
        //     this.pageScope.push(i);
        //   }
        //   return;
        // }
        // if (this.currentPage === this.options.totalPage) {
        //   // 到最后一页时
        //   let startPage: number = Math.max(
        //     this.options.totalPage - (this.options.showPage as number),
        //     1
        //   );
        //   for (let i = startPage; i <= this.options.totalPage; i++) {
        //     this.pageScope.push(i);
        //   }
        //   return;
        // }
        // // 正常情况时
        // if ((this.options.showPage as number) % 2 !== 0) {
        //   // 展示奇数个
        //   let half: number = Math.floor((this.options.showPage as number) / 2);
        //   let startPage: number = Math.max(this.currentPage - half, 1);
        //   let endPage: number = Math.min(
        //     this.currentPage + half,
        //     this.options.totalPage as number
        //   );
        //   for (let i = startPage; i <= endPage; i++) {
        //     this.pageScope.push(i);
        //   }
        // } else {
        //   // 展示个数为偶数
        //   let half: number = Math.floor((this.options.showPage as number) / 2);
        //   let startPage: number = Math.max(this.currentPage - half + 1, 1);
        //   let endPage: number = Math.min(
        //     this.currentPage + half,
        //     this.options.totalPage as number
        //   );
        //   for (let i = startPage; i <= endPage; i++) {
        //     this.pageScope.push(i);
        //   }
        // }
    };
    // 更新DOM内容
    SplitPager.prototype.updateDomContent = function () {
        // 去除所有的active样式
        this.removeElemStyle.apply(this, __spreadArrays([SplitPager.activeStyleName], this.normalElem));
        for (var i = 0; i < this.normalElem.length; i++) {
            var value = this.pageScope[i].toString();
            this.normalElem[i].textContent = value;
            // 设置动态样式
            if (value === this.currentPage.toString())
                this.addElemStyle(SplitPager.activeStyleName, this.normalElem[i]);
            this.normalElem[i].dataset.pagerValue = value;
        }
    };
    // 设定按钮权限
    SplitPager.prototype.setButtonAuthority = function () {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        if (this.currentPage === 1) {
            // 到首页
            (_a = this.firstElem) === null || _a === void 0 ? void 0 : _a.setAttribute("disabled", "");
            (_b = this.prevElem) === null || _b === void 0 ? void 0 : _b.setAttribute("disabled", "");
            this.addElemStyle(SplitPager.bannedStyleName, this.firstElem, this.prevElem);
            this.addElemStyle(SplitPager.bannedStyleName + ':hover', this.firstElem, this.prevElem); // ！移除hover样式
        }
        else {
            (_c = this.firstElem) === null || _c === void 0 ? void 0 : _c.removeAttribute("disabled");
            (_d = this.prevElem) === null || _d === void 0 ? void 0 : _d.removeAttribute("disabled");
            this.removeElemStyle(SplitPager.bannedStyleName, this.firstElem, this.prevElem);
            this.removeElemStyle(SplitPager.bannedStyleName + ':hover', this.firstElem, this.prevElem);
        }
        if (this.currentPage === this.options.totalPage) {
            (_e = this.lastElem) === null || _e === void 0 ? void 0 : _e.setAttribute("disabled", "");
            (_f = this.nextElem) === null || _f === void 0 ? void 0 : _f.setAttribute("disabled", "");
            this.addElemStyle(SplitPager.bannedStyleName, this.lastElem, this.nextElem);
        }
        else {
            (_g = this.lastElem) === null || _g === void 0 ? void 0 : _g.removeAttribute("disabled");
            (_h = this.nextElem) === null || _h === void 0 ? void 0 : _h.removeAttribute("disabled");
            this.removeElemStyle(SplitPager.bannedStyleName, this.lastElem, this.nextElem);
        }
    };
    // 绑定事件方法
    SplitPager.prototype.bindEvents = function () {
        var _a, _b, _c, _d;
        var _this = this;
        function firstHandle() {
            // 首页绑定方法
            _this.jumpToPage(1);
        }
        function prevHandle() {
            // 返回上一页方法
            _this.jumpToPage(_this.currentPage - 1);
        }
        function nextHandle() {
            // 前往下一页方法
            _this.jumpToPage(_this.currentPage + 1);
        }
        function lastHandle() {
            // 前往最后一页方法
            _this.jumpToPage(_this.options.totalPage);
        }
        function normalHandle(e) {
            // 处理普通元素方法
            if (!e.target) {
                return;
            }
            var target = e.target;
            _this.jumpToPage(parseInt(target.dataset.pagerValue || "", 10));
        }
        // 绑定
        (_a = this.firstElem) === null || _a === void 0 ? void 0 : _a.addEventListener("click", firstHandle);
        (_b = this.prevElem) === null || _b === void 0 ? void 0 : _b.addEventListener("click", prevHandle);
        (_c = this.nextElem) === null || _c === void 0 ? void 0 : _c.addEventListener("click", nextHandle);
        (_d = this.lastElem) === null || _d === void 0 ? void 0 : _d.addEventListener("click", lastHandle);
        this.normalElem.forEach(function (elem) {
            elem.addEventListener("click", normalHandle);
        });
    };
    // 跳转页面
    SplitPager.prototype.jumpToPage = function (pageNum) {
        // 安全监测
        pageNum = pageNum < 1 ? 1 : pageNum;
        pageNum =
            pageNum > this.options.totalPage
                ? this.options.totalPage
                : pageNum;
        this.currentPage = pageNum;
        this.setPath();
        this.calNumberScope();
        this.updateDomContent();
        this.setButtonAuthority();
    };
    SplitPager.prototype.setPath = function () {
        if (this.options.pageQuery) { // 如果存在查询参数
            if (this.currentPage === 1) { // 如果当前跳转到首页
                location.pathname = this.rootPath;
            }
            else { // 默认情况
                var queryStr = this.options.pageQuery + this.currentPage;
                location.pathname = location.pathname + "/" + encodeURIComponent(queryStr); // 拼接路径
            }
        }
    };
    // 为一批元素添加样式
    SplitPager.prototype.addElemStyle = function (styleName) {
        var elems = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            elems[_i - 1] = arguments[_i];
        }
        elems.forEach(function (elem) {
            elem === null || elem === void 0 ? void 0 : elem.classList.add(styleName);
        });
    };
    // 为一批元素移除样式
    SplitPager.prototype.removeElemStyle = function (styleName) {
        var elems = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            elems[_i - 1] = arguments[_i];
        }
        elems.forEach(function (elem) {
            elem === null || elem === void 0 ? void 0 : elem.classList.remove(styleName);
        });
    };
    SplitPager.version = "0.0.3";
    SplitPager.mark = "p-mark";
    SplitPager.bannedStyleName = "pager-banned";
    SplitPager.activeStyleName = 'pager-active';
    return SplitPager;
}());
